package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import com.uwlaser.domain.CraftParamDetail;
import com.uwlaser.service.CraftParamDetailService;
import com.uwlaser.service.dto.CraftParamDetailDto;
import com.uwlaser.service.dto.CraftParamDetailQueryParam;
import com.uwlaser.service.mapper.CraftParamDetailMapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author zk-Qin
* @date 2022-05-11
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = CraftParamDetailService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class CraftParamDetailServiceImpl extends CommonServiceImpl<CraftParamDetailMapper, CraftParamDetail> implements CraftParamDetailService {

    // private final RedisUtils redisUtils;
    private final CraftParamDetailMapper craftParamDetailMapper;

    @Override
    public PageInfo<CraftParamDetailDto> queryAll(CraftParamDetailQueryParam query, Pageable pageable) {
        IPage<CraftParamDetail> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<CraftParamDetail> page = craftParamDetailMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, CraftParamDetailDto.class);
    }

    @Override
    public List<CraftParamDetailDto> queryAll(CraftParamDetailQueryParam query){
        return ConvertUtil.convertList(craftParamDetailMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), CraftParamDetailDto.class);
    }

    @Override
    public CraftParamDetail getById(Integer id) {
        return craftParamDetailMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public CraftParamDetailDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), CraftParamDetailDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(CraftParamDetailDto resources) {
        CraftParamDetail entity = ConvertUtil.convert(resources, CraftParamDetail.class);

        CraftParamDetail craftParamDetail = new CraftParamDetail();
        craftParamDetail.setParamsId(entity.getParamsId());
        craftParamDetail.setParamCode(entity.getParamCode());
        Integer integer = craftParamDetailMapper.selectCount(new QueryWrapper<>(craftParamDetail));
        if(integer > 0){
            throw new RuntimeException("参数编码【"+entity.getParamCode()+"】不能重复！！！");
        }
        return craftParamDetailMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(CraftParamDetailDto resources){
        CraftParamDetail entity = ConvertUtil.convert(resources, CraftParamDetail.class);
        int ret = craftParamDetailMapper.updateById(entity);
        // delCaches(resources.id);
        CraftParamDetail craftParamDetail = new CraftParamDetail();
        craftParamDetail.setParamsId(entity.getParamsId());
        craftParamDetail.setParamCode(entity.getParamCode());
        Integer integer = craftParamDetailMapper.selectCount(new QueryWrapper<>(craftParamDetail));
        if(integer >= 2){
            throw new RuntimeException("参数编码【"+entity.getParamCode()+"】不能重复！！！");
        }
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids){
        // delCaches(ids);
        return craftParamDetailMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    @Override
    public Set<CraftParamDetailDto> getParamDetail(Integer pid) {
        return ConvertUtil.convertSet(craftParamDetailMapper.selectLink(pid), CraftParamDetailDto.class);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<CraftParamDetailDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (CraftParamDetailDto craftParamDetail : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("参数集合ID", craftParamDetail.getParamsId());
              map.put("工序编码", craftParamDetail.getOperationCode());
              map.put("参数编码", craftParamDetail.getParamCode());
              map.put("参数名称", craftParamDetail.getParamName());
              map.put("参数下限", craftParamDetail.getParamLower());
              map.put("参数上限", craftParamDetail.getParamUpper());
              map.put("参数单位", craftParamDetail.getParamUnit());
              map.put("标准值", craftParamDetail.getStandard());
              map.put("是否必传", craftParamDetail.getIsNecessary());
              map.put("是否二次校验", craftParamDetail.getIsCheck());
              map.put("描述", craftParamDetail.getRemark());
              map.put("创建人", craftParamDetail.getCreateBy());
              map.put("创建时间", craftParamDetail.getCreateTime());
              map.put("更新人", craftParamDetail.getUpdateBy());
              map.put("更新时间", craftParamDetail.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
